package com.bluetoothsms.android;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

import com.bluetoothsms.common.Common.SmsStatus;
import com.bluetoothsms.common.Common.SmsType;
import com.bluetoothsms.common.Sms;

public class MessageReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {

		Bundle bundle = intent.getExtras();

		Object[] messages = (Object[]) bundle.get("pdus");
		SmsMessage[] sms = new SmsMessage[messages.length];

		for (int n = 0; n < messages.length; n++) {
			sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
		}

		for (SmsMessage msg : sms) {
			MainActivity.onSmsReceived(new Sms(msg.getMessageBody(), msg
					.getTimestampMillis(), SmsType.Received, -1, msg
					.getOriginatingAddress(), SmsStatus.Received));
		}
	}
}
